﻿Imports System.Data.SqlClient
Imports System.Configuration
Public Class FmNewKH
    Dim DA As New SqlDataAdapter
    Dim t As New DataTable

    Private Sub FmNewKH_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TxtMaKH.Text = MaxMaKH()

        Dim dieuphoi As New SqlDataAdapter("Select * from KHACHHANG", KetNoi)

        Dim DTKH As New DataTable
        dieuphoi.Fill(DTKH)
        KetNoi.Close()
        DgvMaKH.DataSource = DTKH

        BindingSource1.DataSource = DTKH
        DgvMaKH.DataSource = BindingSource1

        DgvMaKH.Visible = False
        'CbChonMl.DataSource = DTKH
        'CbChonMl.DisplayMember = "MALOAI"

    End Sub

    Public Function MaxMaKH() As String
        Dim SOMAX As Long
        Dim sql4 As String
        'Dim conn4 As SqlConnection = New SqlConnection(CONSTR)
        'conn4.Open()
        XdKetNoi()

        Dim Cmd As SqlCommand = New SqlCommand()
        Dim prm As SqlParameter = New SqlParameter()
        sql4 = "select max(CAST(RIGHT(MAKH,2) AS INT)) AS SOMAX from KHACHHANG"
        Cmd.Connection = KetNoi
        Cmd.CommandType = CommandType.Text
        Cmd.CommandText = sql4
        SOMAX = Cmd.ExecuteScalar()
        Dim stt As String
        'stt = "001"
        'Dim SOMAX As Integer = CType(Cmd.ExecuteScalar().ToString, Integer) + 1
        If SOMAX < 10 Then
            stt = "00" + CType(SOMAX + 1, String)
        ElseIf SOMAX >= 10 And SOMAX < 100 Then
            stt = "0" + CType(SOMAX + 1, String)
        Else
            stt = CType(SOMAX + 1, String)
        End If
        MaxMaKH = "K" + stt
    End Function

    Public Sub ThemKH()
        Dim Result As Integer

        Dim sql4 As String
        XdKetNoi()

        Dim Cmd As SqlCommand = New SqlCommand()
        Dim prm As SqlParameter = New SqlParameter()
        sql4 = "insert into KHACHHANG (MAKH,TENKH,DIACHI,SDT,MASOTHUE,SOTK) VALUES('" & TxtMaKH.Text & "', '" & TxtTenKH.Text & "', '" & TxtDiaChi.Text & "', '" & TxtSDT.Text & "', '" & TxtMST.Text & "' ,'" & TxtSoTK.Text & "')"
        Cmd.Connection = KetNoi
        Cmd.CommandType = CommandType.Text
        Cmd.CommandText = sql4
        Try
            Result = Cmd.ExecuteNonQuery()
            MessageBox.Show("Thêm thành công")
            Me.Close()




        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BtThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtThem.Click
        If TxtSoTK.Text <> "" And TxtTenKH.Text <> "" And TxtDiaChi.Text <> "" And TxtSDT.Text <> "" And TxtMST.Text <> "" Then
            ThemKH()
        Else
            MsgBox("không đủ thông tin")
        End If
    End Sub


    Private Sub BtHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtHuy.Click
        TxtDiaChi.Clear()
        TxtMaKH.Clear()
        TxtMST.Clear()
        TxtSDT.Clear()
        TxtSoTK.Clear()
        TxtTenKH.Clear()
    End Sub

    Private Sub BtThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtThoat.Click
        Me.Close()
    End Sub
End Class